<!--
 * @Descripttion: (用户表/lay_user)
 * @Author: (admin)
 * @Date: (2024-10-07)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="搜索条件" prop="name">
        <el-input v-model="queryParams.name" placeholder="微信名/姓名" />
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['layuser:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['layuser:edit']" plain icon="edit"
          @click="handleUpdate">
          {{ $t('btn.edit') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['layuser:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
      highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column prop="applyStatusName" label="审批状态名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('applyStatusName')" />
      <el-table-column prop="hasAmount" label="可用金额" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('hasAmount')" />
      <el-table-column prop="talScore" label="总积分" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('talScore')" />
      <el-table-column prop="hasScore" label="可用积分" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('hasScore')" />
      <el-table-column prop="reCode" label="推荐码" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('reCode')" />
      <el-table-column prop="refereOpenid" label="关联上级openid" align="center"
        v-if="columns.showColumn('refereOpenid')" />
      <el-table-column prop="refereId" label="上级id" align="center" v-if="columns.showColumn('refereId')" />
      <el-table-column prop="companyId" label="商家id" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('companyId')" />
      <el-table-column prop="companyName" label="商家名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('companyName')" />
      <el-table-column prop="applyStatus" label="审批状态" align="center" v-if="columns.showColumn('applyStatus')">
        <template #default="scope">
          <dict-tag :options="options.applyStatusOptions" :value="scope.row.applyStatus" />
        </template>
      </el-table-column>
      <el-table-column prop="talAmount" label="总金额" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('talAmount')" />
      <el-table-column prop="applyRemark" label="审批备注" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('applyRemark')" />
      <el-table-column prop="applyTime" label="审批时间" :show-overflow-tooltip="true"
        v-if="columns.showColumn('applyTime')" />
      <el-table-column prop="status" label="状态id" align="center" v-if="columns.showColumn('status')">
        <template #default="scope">
          <dict-tag :options="options.statusOptions" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column prop="statusName" label="状态" align="center" v-if="columns.showColumn('statusName')">
        <template #default="scope">
          <dict-tag :options="options.statusNameOptions" :value="scope.row.statusName" />
        </template>
      </el-table-column>
      <el-table-column prop="nickName" label="微信名" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('nickName')" />
      <el-table-column prop="trueName" label="姓名" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('trueName')" />
      <el-table-column prop="birtday" label="生日" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('birtday')">
        <template #default="scope">
          {{ dayjs(scope.row.birtday).format("YYYY-MM-DD") }}
        </template>
      </el-table-column>
      <el-table-column prop="tel" label="电话" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('tel')" />
      <el-table-column prop="photo" label="头像" align="center" v-if="columns.showColumn('photo')">
        <template #default="scope">
          <ImagePreview :src="scope.row.photo"></ImagePreview>
        </template>
      </el-table-column>
      <el-table-column prop="sex" label="性别" align="center" v-if="columns.showColumn('sex')">
        <template #default="scope">
          <dict-tag :options="options.sexOptions" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column prop="unionid" label="Unionid" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('unionid')" />
      <el-table-column prop="openid" label="openid" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('openid')" />
      <el-table-column prop="province" label="省级" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('province')" />
      <el-table-column prop="city" label="市" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('city')" />
      <el-table-column prop="area" label="区" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('area')" />
      <el-table-column prop="jiedao" label="街道" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('jiedao')" />
      <el-table-column prop="address" label="地址" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('address')" />

      <el-table-column prop="id" label="id" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('id')" />
      <el-table-column prop="cardNo" label="身份证" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('cardNo')" />



      <el-table-column prop="grade" label="等级" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('grade')" />
      <el-table-column prop="gradeName" label="等级名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('gradeName')" />
      <el-table-column prop="gradePerc" label="等级折扣" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('gradePerc')" />
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="primary" size="small" icon="view" title="详情" @click="handlePreview(scope.row)"></el-button>
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['layuser:edit']"
            @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['layuser:delete']"
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />


    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">


          <!-- <el-col :lg="12">
            <el-form-item label="可用金额" prop="hasAmount">
              <el-input v-model="form.hasAmount" placeholder="请输入可用金额" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="总积分" prop="talScore">
              <el-input v-model="form.talScore" placeholder="请输入总积分" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="可用积分" prop="hasScore">
              <el-input v-model="form.hasScore" placeholder="请输入可用积分" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="推荐码" prop="reCode">
              <el-input v-model="form.reCode" placeholder="请输入推荐码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="关联上级openid" prop="refereOpenid">
              <el-input v-model.number="form.refereOpenid" placeholder="请输入关联上级openid" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="上级id" prop="refereId">
              <el-input v-model.number="form.refereId" placeholder="请输入上级id" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="商家id" prop="companyId">
              <el-input v-model="form.companyId" placeholder="请输入商家id" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="商家名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入商家名称" />
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="审批状态" prop="applyStatus">
              <el-radio-group v-model="form.applyStatus">
                <el-radio v-for="item in options.applyStatus" :key="item.dictValue" :value="item.dictValue">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="总金额" prop="talAmount">
              <el-input v-model="form.talAmount" placeholder="请输入总金额" />
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="审批备注" prop="applyRemark">
              <el-input v-model="form.applyRemark" placeholder="请输入审批备注" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="审批时间" prop="applyTime">
              <el-date-picker v-model="form.applyTime" type="datetime" placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col> -->



          <!-- 
          <el-col :lg="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-input v-model="form.createTime" placeholder="请输入创建时间" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建人" prop="createBy">
              <el-input v-model="form.createBy" placeholder="请输入创建人" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新时间" prop="updateTime">
              <el-input v-model="form.updateTime" placeholder="请输入更新时间" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新人" prop="updateBy">
              <el-date-picker v-model="form.updateBy" type="datetime" placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col> -->

          <el-col :lg="6">
            <el-form-item label="微信名" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入微信名" />
            </el-form-item>
          </el-col>
          <el-col :lg="6">
            <el-form-item label="生日" prop="birtday">
              <el-date-picker v-model="form.birtday" type="date" placeholder="选择生日" value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="6">
            <el-form-item label="姓名" prop="trueName">
              <el-input v-model="form.trueName" placeholder="请输入真名" />
            </el-form-item>
          </el-col>
          <el-col :lg="6">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option v-for="item in options.sys_user_sex" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :lg="6">
            <el-form-item label="省级" prop="province">
              <el-input v-model="form.province" placeholder="请输入省级" />
            </el-form-item>
          </el-col>

          <el-col :lg="6">
            <el-form-item label="市" prop="city">
              <el-input v-model="form.city" placeholder="请输入市" />
            </el-form-item>
          </el-col>

          <el-col :lg="6">
            <el-form-item label="区" prop="area">
              <el-input v-model="form.area" placeholder="请输入区" />
            </el-form-item>
          </el-col>

          <el-col :lg="6">
            <el-form-item label="街道" prop="jiedao">
              <el-input v-model="form.jiedao" placeholder="请输入街道" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>

          <el-col :lg="6">
            <el-form-item label="电话" prop="tel">
              <el-input v-model="form.tel" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
          <el-col :lg="6">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status">
                <el-option v-for="item in options.status" :key="item.dictValue" :value="item.dictValue"
                  :label="item.dictLabel">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <!-- <el-col :lg="12">
            <el-form-item label="id" prop="id">
              <el-input v-model="form.id" placeholder="请输入id" :disabled="opertype != 1" />
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="身份证" prop="cardNo">
              <el-input v-model="form.cardNo" placeholder="请输入身份证" />
            </el-form-item>
          </el-col> -->

          <el-col :lg="12">
            <el-form-item label="Unionid" prop="unionid">
              <el-input v-model="form.unionid" placeholder="请输入Unionid" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="openid" prop="openid">
              <el-input v-model="form.openid" placeholder="请输入openid" />
            </el-form-item>
          </el-col>


          <el-col :lg="24">
            <el-form-item label="头像" prop="photo">
              <UploadImage v-model="form.photo" :data="{ uploadType: 1 }" />
            </el-form-item>
          </el-col>




          <!-- <el-col :lg="12">
            <el-form-item label="等级" prop="grade">
              <el-input v-model="form.grade" placeholder="请输入等级" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="等级名称" prop="gradeName">
              <el-input v-model="form.gradeName" placeholder="请输入等级名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="等级折扣" prop="gradePerc">
              <el-input v-model="form.gradePerc" placeholder="请输入等级折扣" />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="layuser">
import {
  listLayUser,
  addLayUser, delLayUser,
  updateLayUser, getLayUser,
}
  from '@/api/business/layuser.js'
import { dayjs } from 'element-plus';
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: false, align: 'center', type: '', prop: 'applyStatusName', label: '审批状态名称', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'hasAmount', label: '可用金额', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'talScore', label: '总积分', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'hasScore', label: '可用积分', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'reCode', label: '推荐码', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'refereOpenid', label: '关联上级openid' },
  { visible: false, align: 'center', type: '', prop: 'refereId', label: '上级id' },
  { visible: false, align: 'center', type: '', prop: 'companyId', label: '商家id', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'companyName', label: '商家名称', showOverflowTooltip: true },
  { visible: false, align: 'center', type: 'dict', prop: 'applyStatus', label: '审批状态', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'talAmount', label: '总金额', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'applyRemark', label: '审批备注', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'applyTime', label: '审批时间', showOverflowTooltip: true },
  { visible: false, align: 'center', type: 'dict', prop: 'status', label: '状态id', showOverflowTooltip: true },
  { visible: false, align: 'center', type: 'dict', prop: 'statusName', label: '状态', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'nickName', label: '微信名', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'unionid', label: 'Unionid', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'openid', label: 'openid', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'province', label: '省级', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'city', label: '市', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'area', label: '区', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'jiedao', label: '街道', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'address', label: '地址', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'tel', label: '电话', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'id', label: 'id', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'cardNo', label: '身份证', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'birtday', label: '生日', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'trueName', label: '真名', showOverflowTooltip: true },
  { visible: true, align: 'center', type: 'img', prop: 'photo', label: '头像', showOverflowTooltip: true },
  { visible: true, align: 'center', type: 'dict', prop: 'sex', label: '性别', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'grade', label: '等级', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'gradeName', label: '等级名称', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'gradePerc', label: '等级折扣', showOverflowTooltip: true },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


function getList() {
  loading.value = true
  listLayUser(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1
  multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
  },
  options: {
    // 审批状态 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    applyStatusOptions: [],
    status: [],
    sys_user_sex: [],
  }
})

var dictParams = [
  "status", 'sys_user_sex'
]

proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.dictType] = element.list
  })
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    applyStatusName: null,
    hasAmount: null,
    talScore: null,
    hasScore: null,
    reCode: null,
    refereOpenid: null,
    refereId: null,
    companyId: null,
    companyName: null,
    applyStatus: null,
    talAmount: null,
    applyRemark: null,
    applyTime: null,
    status: null,
    statusName: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    nickName: null,
    unionid: null,
    openid: null,
    province: null,
    city: null,
    area: null,
    jiedao: null,
    address: null,
    tel: null,
    id: null,
    cardNo: null,
    birtday: null,
    trueName: null,
    photo: null,
    sex: null,
    grade: null,
    gradeName: null,
    gradePerc: null,
  };
  proxy.resetForm("formRef")
}

/**
 * 查看
 * @param {*} row
 */
function handlePreview(row) {
  reset()
  const id = row.id
  getLayUser(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '查看'
      opertype.value = 3
      form.value = {
        ...data,
      }
    }
  })
}

// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加用户表'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getLayUser(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改用户表'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateLayUser(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLayUser(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delLayUser(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}




handleQuery()
</script>